/// 聊天消息实体模型
/// 
/// 🎯 支持三种消息类型：
/// 1. chat - 普通好友聊天消息
/// 2. friend_request - 好友申请消息
/// 3. activity_request - 活动申请消息
class ChatMessageEntity {
  /// 消息内容
  final String content;
  
  /// 是否是我发送的
  final bool isMe;
  
  /// 消息时间戳（毫秒）
  final int timestamp;
  
  /// 发送者标识：1=我，2=好友
  final String sender;
  
  /// 消息是否已读：0=未读，1=已读
  final String messageIfRead;
  
  /// 消息唯一ID
  final String? messageId;
  
  /// 发送者姓名
  final String? senderName;
  
  /// 发送者头像URL
  final String? senderAvatar;
  
  /// 🔥 消息类型：chat, friend_request, activity_request
  final String messageType;
  
  /// 🔥 关联的用户ID（用于消息分组）
  final String userId;

  ChatMessageEntity({
    required this.content,
    required this.isMe,
    required this.timestamp,
    required this.userId,
    String? sender,
    this.messageIfRead = "1",
    this.messageId,
    this.senderName,
    this.senderAvatar,
    this.messageType = 'chat',
  }) : sender = sender ?? (isMe ? "1" : "2");

  /// 从JSON创建消息实体
  factory ChatMessageEntity.fromJson(Map<String, dynamic> json) {
    return ChatMessageEntity(
      content: json['content'] ?? '',
      isMe: json['isMe'] ?? false,
      timestamp: json['timestamp'] is int 
          ? json['timestamp'] 
          : DateTime.tryParse(json['timestamp'] ?? '')?.millisecondsSinceEpoch ?? 0,
      userId: json['userId'] ?? '',
      sender: json['sender'] ?? "2",
      messageIfRead: json['messageIfRead'] ?? "1",
      messageId: json['messageId'],
      senderName: json['senderName'],
      senderAvatar: json['senderAvatar'],
      messageType: json['messageType'] ?? 'chat',
    );
  }

  /// 转换为JSON（用于本地存储）
  Map<String, dynamic> toJson() {
    return {
      'content': content,
      'isMe': isMe,
      'timestamp': timestamp,
      'userId': userId,
      'sender': sender,
      'messageIfRead': messageIfRead,
      'messageId': messageId,
      'senderName': senderName,
      'senderAvatar': senderAvatar,
      'messageType': messageType,
    };
  }

  /// 转换为Map格式（用于内存存储）
  Map<String, dynamic> toMap() {
    return toJson();
  }

  /// 获取格式化的时间
  DateTime get dateTime => DateTime.fromMillisecondsSinceEpoch(timestamp);

  /// 判断是否是系统消息（好友申请或活动申请）
  bool get isSystemMessage => messageType != 'chat';

  /// 判断是否是好友申请消息
  bool get isFriendRequest => messageType == 'friend_request';

  /// 判断是否是活动申请消息
  bool get isActivityRequest => messageType == 'activity_request';

  /// 创建副本
  ChatMessageEntity copyWith({
    String? content,
    bool? isMe,
    int? timestamp,
    String? userId,
    String? sender,
    String? messageIfRead,
    String? messageId,
    String? senderName,
    String? senderAvatar,
    String? messageType,
  }) {
    return ChatMessageEntity(
      content: content ?? this.content,
      isMe: isMe ?? this.isMe,
      timestamp: timestamp ?? this.timestamp,
      userId: userId ?? this.userId,
      sender: sender ?? this.sender,
      messageIfRead: messageIfRead ?? this.messageIfRead,
      messageId: messageId ?? this.messageId,
      senderName: senderName ?? this.senderName,
      senderAvatar: senderAvatar ?? this.senderAvatar,
      messageType: messageType ?? this.messageType,
    );
  }

  @override
  String toString() {
    return 'ChatMessageEntity(content: $content, isMe: $isMe, messageType: $messageType, userId: $userId)';
  }
}

/// 未读消息统计实体
class UnreadMessageCount {
  /// 好友聊天消息未读数
  final int chatUnreadCount;
  
  /// 好友申请消息未读数
  final int friendRequestUnreadCount;
  
  /// 活动申请消息未读数
  final int activityRequestUnreadCount;
  
  /// 总未读数
  int get totalUnreadCount => chatUnreadCount + friendRequestUnreadCount + activityRequestUnreadCount;

  UnreadMessageCount({
    this.chatUnreadCount = 0,
    this.friendRequestUnreadCount = 0,
    this.activityRequestUnreadCount = 0,
  });

  /// 从JSON创建
  factory UnreadMessageCount.fromJson(Map<String, dynamic> json) {
    return UnreadMessageCount(
      chatUnreadCount: json['chatUnreadCount'] ?? 0,
      friendRequestUnreadCount: json['friendRequestUnreadCount'] ?? 0,
      activityRequestUnreadCount: json['activityRequestUnreadCount'] ?? 0,
    );
  }

  /// 转换为JSON
  Map<String, dynamic> toJson() {
    return {
      'chatUnreadCount': chatUnreadCount,
      'friendRequestUnreadCount': friendRequestUnreadCount,
      'activityRequestUnreadCount': activityRequestUnreadCount,
      'totalUnreadCount': totalUnreadCount,
    };
  }

  /// 创建副本
  UnreadMessageCount copyWith({
    int? chatUnreadCount,
    int? friendRequestUnreadCount,
    int? activityRequestUnreadCount,
  }) {
    return UnreadMessageCount(
      chatUnreadCount: chatUnreadCount ?? this.chatUnreadCount,
      friendRequestUnreadCount: friendRequestUnreadCount ?? this.friendRequestUnreadCount,
      activityRequestUnreadCount: activityRequestUnreadCount ?? this.activityRequestUnreadCount,
    );
  }

  @override
  String toString() {
    return 'UnreadMessageCount(chat: $chatUnreadCount, friend: $friendRequestUnreadCount, activity: $activityRequestUnreadCount, total: $totalUnreadCount)';
  }
}

